Home:ALL Converter>Attempting to draw in a JPanel from a SwingWorker done() method does nothing

Attempting to draw in a JPanel from a SwingWorker done() method does nothing

Ask Time:2015-03-31T03:04:51         Author:Redoman

Json Formatter

I'm trying to paint a JPanel using Graphics2D, and I'd like the drawing to happen in a SwingWorker's done() method, after some math in doInBackGround(), but this seems to not draw anything?

What is wrong in my code?

class MyJanel extends JPanel  {

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(800, 600);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        MyExecutor worker = new MyExecutor(g2);
        worker.execute();
    }

    class MyExecutor extends SwingWorker<Void,Void> { 

        Graphics2D gRef;
        int result;

        public MyExecutor(Graphics2D g2){
            gRef = g2;
        }

        @Override
        protected Void doInBackground() {
            result = 100+100;
            return null;
        }

        @Override
        protected void done() {
            gRef.drawLine(20, 20, result, result);
        }
    }

}

Author:Redoman,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/29354177/attempting-to-draw-in-a-jpanel-from-a-swingworker-done-method-does-nothing
yy